# This file handles building each task as sub-project.
#
# Each task becomes an independent project in which you can experiment.
#
# Tasks are added as separate sub-projects so as compilation errors in
# one task don't affect another task.

cmake_minimum_required(VERSION 3.12)
project(mylog)

#增加是否编译例子的选项，属于缓存变量
option(BUILD_EXAMPLE "if or not to build example targets" ON)

#设置默认的库文件输出目录，包含静态库和动态库
if(NOT LIBRARY_OUTPUT_PATH)
    set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
endif ()

#设置默认的可执行程序输出目录, CMAKE_BINARY_DIR表示顶层编译目录
if(NOT EXECUTABLE_OUTPUT_PATH)
    set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
endif ()

set(SRC_PATH ${PROJECT_SOURCE_DIR}/src)
set(SRC_MYLOG_PATH ${SRC_PATH}/${PROJECT_NAME})

set(PROJECT_SOURCES
        ${SRC_MYLOG_PATH}/my_log.cpp
        ${SRC_MYLOG_PATH}/my_log.h
        ${SRC_MYLOG_PATH}/log.h
)

#设置C++标准的建议最高支持等级
set(CMAKE_CXX_STANDARD 14)
#强制使用CMAKE_CXX_STANDARD标准，不支持会报错
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#创建库文件，可通过改变BUILD_SHARED_LIBS来空值默认输出的是否是动态库
add_library(${PROJECT_NAME} ${PROJECT_SOURCES})
#为当前的目标包含指定的宏定义
target_compile_definitions(${PROJECT_NAME} PRIVATE MYLOG_LIBRARY)

if(BUILD_SHARED_LIBS)
    set(PROJECT_OUTPUT_NAME ${PROJECT_NAME})
else()
    set(PROJECT_OUTPUT_NAME ${PROJECT_NAME}_static)
endif()

# 设置属性
set_target_properties(${PROJECT_NAME} PROPERTIES
    OUTPUT_NAME ${PROJECT_NAME}
    CLEAN_DIRECT_OUTPUT 1
    VERSION 1.2
    SOVERSION 3
)


#为当前目标包含指定的头文件路径，PUBLIC表示外部依赖此目标的目标环境也会共享此接口路径
target_include_directories(${PROJECT_NAME}
    PUBLIC
        $<INSTALL_INTERFACE:include>
        $<BUILD_INTERFACE:${SRC_PATH}>
)

#安装目标 RUNTIME表示可执行程序，LIBRARY表示动态库，ARCHIVE表示静态库，DESTINATION表示路径
install(TARGETS ${PROJECT_NAME}
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
)

#安装指定的文件
set(EXPORT_INCLUDES ${SRC_MYLOG_PATH}/log.h)
install(FILES ${EXPORT_INCLUDES}
    DESTINATION include/${PROJECT_NAME}
)

if(BUILD_EXAMPLE)
    add_subdirectory(example)
endif ()
